package com.amgl.recyclerviewwithheaderfooter.footer;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewStub;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amgl.recyclerviewwithheaderfooter.R;

/**
 * Created by amglh on 2016/8/10.
 */
public class DefaultFooterView extends RelativeLayout implements ILoadingView {
    public static final String TAG = DefaultFooterView.class.getSimpleName();

    private View mLoadingView;
    private View mNetworkErrorView;
    private View mTheEndView;
    private View mNormalView;

    private TextView mTextError;


    public DefaultFooterView(Context context) {
        super(context);
        init(context);
    }

    public DefaultFooterView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public DefaultFooterView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public void init(Context context) {
        inflate(context, R.layout.default_loading_footer, this);
    }

    @Override
    public void showNormalView() {
        Log.d(TAG, "show footer normal");
        if (mLoadingView != null) {
            mLoadingView.setVisibility(GONE);
        }

        if (mTheEndView != null) {
            mTheEndView.setVisibility(GONE);
        }

        if (mNetworkErrorView != null) {
            mNetworkErrorView.setVisibility(GONE);
        }

        if (mNormalView == null) {
            ViewStub viewStub = (ViewStub) findViewById(R.id.vs_normal);
            mNormalView = viewStub.inflate();
        } else {
            mNormalView.setVisibility(VISIBLE);
        }
    }

    @Override
    public void showLoadingView() {
        Log.d(TAG, "show footer loading");
        if (mNormalView != null) {
            mNormalView.setVisibility(GONE);
        }

        if (mTheEndView != null) {
            mTheEndView.setVisibility(GONE);
        }

        if (mNetworkErrorView != null) {
            mNetworkErrorView.setVisibility(GONE);
        }

        if (mLoadingView == null) {
            ViewStub viewStub = (ViewStub) findViewById(R.id.vs_loading);
            mLoadingView = viewStub.inflate();
        } else {
            mLoadingView.setVisibility(VISIBLE);
        }
    }


    @Override
    public void showEndView() {
        Log.d(TAG, "show footer end");
        if (mNormalView != null) {
            mNormalView.setVisibility(GONE);
        }

        if (mLoadingView != null) {
            mLoadingView.setVisibility(GONE);
        }

        if (mNetworkErrorView != null) {
            mNetworkErrorView.setVisibility(GONE);
        }

        if (mTheEndView == null) {
            ViewStub viewStub = (ViewStub) findViewById(R.id.vs_end);
            mTheEndView = viewStub.inflate();
        } else {
            mTheEndView.setVisibility(VISIBLE);
        }
    }

    @Override
    public void showErrorView(String errorMessage) {
        Log.d(TAG, "show footer error");
        if (mNormalView != null) {
            mNormalView.setVisibility(GONE);
        }

        if (mLoadingView != null) {
            mLoadingView.setVisibility(GONE);
        }

        if (mTheEndView != null) {
            mTheEndView.setVisibility(GONE);
        }

        if (mNetworkErrorView == null) {
            ViewStub viewStub = (ViewStub) findViewById(R.id.vs_error);
            mNetworkErrorView = viewStub.inflate();

            mTextError = (TextView) mNetworkErrorView.findViewById(R.id.text_error);
            mTextError.setText(errorMessage);
        } else {
            mNetworkErrorView.setVisibility(VISIBLE);
        }
    }
}
